package com.tools.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tools.pojo.data.MedicalAnalysisRequestDTO;
import com.tools.pojo.data.PatientDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
@RequiredArgsConstructor
public class DTOConverter {

    private final ObjectMapper objectMapper;

    /**
     * 将JSON字符串转换为MedicalAnalysisRequestDTO
     */
    public MedicalAnalysisRequestDTO convertToMedicalAnalysisRequest(String jsonData)
            throws JsonProcessingException {

        log.debug("开始转换JSON数据为DTO对象");

        // 使用Jackson直接反序列化
        MedicalAnalysisRequestDTO requestDTO = objectMapper.readValue(
                jsonData, MedicalAnalysisRequestDTO.class);

        log.debug("JSON数据转换完成，共 {} 个患者",
                requestDTO.getPatientDtos() != null ? requestDTO.getPatientDtos().size() : 0);

        return requestDTO;
    }

    /**
     * 将Map转换为MedicalAnalysisRequestDTO
     */
    public MedicalAnalysisRequestDTO convertToMedicalAnalysisRequest(Map<String, Object> dataMap) {
        log.debug("开始转换Map数据为DTO对象");

        // 使用Jackson进行转换
        MedicalAnalysisRequestDTO requestDTO = objectMapper.convertValue(
                dataMap, MedicalAnalysisRequestDTO.class);

        log.debug("Map数据转换完成，共 {} 个患者",
                requestDTO.getPatientDtos() != null ? requestDTO.getPatientDtos().size() : 0);

        return requestDTO;
    }

    /**
     * 将DTO对象转换为Map
     */
    public Map<String, Object> convertToMap(MedicalAnalysisRequestDTO requestDTO) {
        log.debug("开始转换DTO对象为Map");

        // 使用Jackson进行转换
        Map<String, Object> dataMap = objectMapper.convertValue(
                requestDTO, Map.class);

        log.debug("DTO对象转换完成");
        return dataMap;
    }

    /**
     * 验证DTO对象的完整性
     */
    public ValidationResult validateMedicalAnalysisRequest(MedicalAnalysisRequestDTO requestDTO) {
        ValidationResult result = new ValidationResult();

        if (requestDTO == null) {
            result.setValid(false);
            result.setMessage("请求数据不能为空");
            return result;
        }

        // 验证必填字段
        if (requestDTO.getSysCode() == null || requestDTO.getSysCode().trim().isEmpty()) {
            result.setValid(false);
            result.setMessage("系统代码不能为空");
            return result;
        }

        if (requestDTO.getPatientDtos() == null || requestDTO.getPatientDtos().isEmpty()) {
            result.setValid(false);
            result.setMessage("患者数据不能为空");
            return result;
        }

        if (requestDTO.getRuleIds() == null || requestDTO.getRuleIds().isEmpty()) {
            result.setValid(false);
            result.setMessage("规则ID列表不能为空");
            return result;
        }

        if (requestDTO.getTaskId() == null || requestDTO.getTaskId().trim().isEmpty()) {
            result.setValid(false);
            result.setMessage("任务ID不能为空");
            return result;
        }

        // 验证患者数据的完整性
        for (int i = 0; i < requestDTO.getPatientDtos().size(); i++) {
            PatientDTO patient = requestDTO.getPatientDtos().get(i);
            ValidationResult patientValidation = validatePatient(patient, i);
            if (!patientValidation.isValid()) {
                return patientValidation;
            }
        }

        result.setValid(true);
        result.setMessage("数据验证通过");
        return result;
    }

    private ValidationResult validatePatient(PatientDTO patient, int index) {
        if (patient.getPatnId() == null || patient.getPatnId().trim().isEmpty()) {
            return ValidationResult.invalid("第 " + (index + 1) + " 个患者ID不能为空");
        }

        if (patient.getPatnName() == null || patient.getPatnName().trim().isEmpty()) {
            return ValidationResult.invalid("第 " + (index + 1) + " 个患者姓名不能为空");
        }

        if (patient.getGend() == null || patient.getGend().trim().isEmpty()) {
            return ValidationResult.invalid("第 " + (index + 1) + " 个患者性别不能为空");
        }

        if (patient.getBrdy() == null || patient.getBrdy().trim().isEmpty()) {
            return ValidationResult.invalid("第 " + (index + 1) + " 个患者出生日期不能为空");
        }

        return ValidationResult.valid();
    }
}